package com.seres.response;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.seres.enums.ErrorEnum;
import com.seres.exception.ServerException;
import com.seres.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/*
 * @author Tan-Bowen
 * @description TODO 对外提接口提供的公共实体返回基类
 * @date 2021/7/22 15:20
 */
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponse<T> {
    /***
     * 状态码
     */
    private int code;
    /***
     * 错误提示
     */
    private String msg;

    /**
     * 业务数据
     */
    private T data;

    public ApiResponse<T> code(int code) {
        this.code = code;
        return this;
    }

    public ApiResponse<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public ApiResponse<T> data(T data) {
        this.data = data;
        return this;
    }


    /**
     * @param [errorEnum, data]
     * @return com.wen.common.response.ApiResponse<?>
     * @author Tan-Bowen
     * @description TODO
     * @date 2021/7/22 15:19
     * @paramType [com.wen.common.enums.ErrorEnum, java.lang.Object]
     */
    private static <T> ApiResponse<T> create(ErrorEnum errorEnum, T data) {
        return create(errorEnum.getCode(), errorEnum.getMsg(), data);
    }


    public static <T> ApiResponse<T> create(int code, String msg, T data) {
        return new ApiResponse<T>().code(code).msg(msg).data(data);
    }

    /**
     * @param []
     * @return com.wen.common.response.ApiResponse<?>
     * @author Tan-Bowen
     * @description TODO 无业务数据返回结构
     * @date 2021/7/22 15:19
     * @paramType []
     */
    public static <T> ApiResponse<T> success() {
        return create(ErrorEnum.SUCCESS, null);
    }

    /**
     * @param [data]
     * @return com.wen.common.response.ApiResponse<?>
     * @author Tan-Bowen
     * @description TODO 有业务数据返回接口
     * @date 2021/7/22 15:19
     * @paramType [java.lang.Object]
     */
    public static <T> ApiResponse<T> success(T data) {
        return create(ErrorEnum.SUCCESS, data);
    }

    /**
     * @param []
     * @return com.wen.common.response.ApiResponse<?>
     * @author Tan-Bowen
     * @description TODO 不指定错误编码的系统错误 默认返回 500
     * @date 2021/7/22 15:19
     * @paramType []
     */
    public static <T> ApiResponse<T> error() {
        return create(ErrorEnum.SERVER_ERROR, null);
    }

    /**
     * @param [errorEnum]
     * @return com.wen.common.response.ApiResponse<?>
     * @author Tan-Bowen
     * @description TODO 指定业务编码的错误返回
     * @date 2021/7/22 15:19
     * @paramType [com.wen.common.enums.ErrorEnum]
     */
    public static <T> ApiResponse<T> error(ErrorEnum errorEnum) {
        return create(errorEnum, null);
    }


    /**
     * @param [e]
     * @return com.demo.core.response.ApiResponse<T>
     * @author Tan-Bowen
     * @description TODO 公用code,自定义message
     * @date 2021/8/12 16:18
     * @paramType [com.demo.core.exception.ServerException]
     */
    public static <T> ApiResponse<T> error(ServerException e) {
        String message = e.getErrorEnum().getMsg();
        if (StringUtils.isNotBlank(e.getMessage())) {
            message = e.getMessage();
        }
        return create(e.getErrorEnum().getCode(), message, null);
    }

    /**
     * @param [msg]
     * @return com.wen.common.response.ApiResponse
     * @author Tan-Bowen
     * @description TODO 参数错误专用
     * @date 2021/7/22 15:18
     * @paramType [java.lang.String]
     */
    public static <T> ApiResponse<T> paramError(String msg) {
        return create(ErrorEnum.PARAM_ERROR.getCode(), msg, null);
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ErrorEnum.SUCCESS.getCode();
    }

}
